package org.mobile.beans;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;

import org.mobile.logic.OrderLogic;
import org.mobile.logic.UserLogic;

public class User {
	private String login;
	private String password;
	private String repeatPass;
	private String name;
	private String surname;
	private int id;
	private String type;
	private String address;
	private int style;
	public static final int DEFAULT_THEME= 1;
	public static final String TYPE_USER= "User";
	public static final String TYPE_ADMIN= "Admin";
	private ArrayList<Order> userOrders= new ArrayList<Order>();
	private User selectedUser;
	private User loggedUser;

	
	public String getLogin() {
		return login;
	}
	public void setLogin(String login) {
		this.login = login;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSurname() {
		return surname;
	}
	public void setSurname(String surname) {
		this.surname = surname;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getType() {
		return type; 
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public ArrayList<Order> getUserOrders() {
		return userOrders;
	}
	public void setUserOrders(ArrayList<Order> userOrders) {
		this.userOrders = userOrders;
	}
	public int getStyle() {
		return style;
	}
	public void setStyle(int style) {
		this.style = style;
	}
	
	public User getSelectedUser() {
		return selectedUser;
	}
	public void setSelectedUser(User selectedUser) {
		this.selectedUser = selectedUser;
	}
	
	public String getRepeatPass() {
		return repeatPass;
	}
	public void setRepeatPass(String repeatPass) {
		this.repeatPass = repeatPass;
	}
		
	public User getLoggedUser() {
		return loggedUser;
	}
	public void setLoggedUser(User loggedUser) {
		this.loggedUser = loggedUser;
	}
	public String log_in(){
		User user = UserLogic.getInstance().logIn(this.login, this.password);
		if(user != null){
			this.loggedUser= user;
			clearData();
			if(user.type.equals(TYPE_ADMIN)){
				List orders= OrderLogic.getInstance().getAllOrders();
				FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("orders", orders);
			}
			else
				loggedUser.setUserOrders((ArrayList<Order>) OrderLogic.getInstance().getOrders(user.id));	
			FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("user", this);
			return "success";
		}
		else{
			FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("error", "Logowanie nie powiodło się");
			clearData();
			return "failure";	
		}
	}
	
	public String register(){
		style= DEFAULT_THEME;
		type= TYPE_USER;
		if(!password.equals(repeatPass)){
			FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("error", "Hasła nie są takie same");
			return "failure";
		}		
		boolean result= UserLogic.getInstance().addUser(this);
		FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("user", null);
		if(result)
			return "success";
		else {
			FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("error", "Rejestracja nie powiodła się");
			return "failure";
		}
	}
	
	public String logOut(){
		Map sessionMap= FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
		sessionMap.put("user", null);
		sessionMap.put("orders", null);
		sessionMap.put("cart", null);
		return "index.jsp?faces-redirect=true";
	}
	
	public String editUser(){
		boolean result= UserLogic.getInstance().updateUser(this.loggedUser);
		if(result){
			List userList= UserLogic.getInstance().getUsers();
			FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("users", userList);
			return "success";
			
		}
		else{
			FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("error", "Edycja nie powiodła się");
			return "failure";	
		}
	}
	
	public String editProfile(){
		boolean result= UserLogic.getInstance().updateUser(selectedUser);
		if(result){
			List userList= UserLogic.getInstance().getUsers();
			FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("users", userList);
			return "success";
		}
		else{
			FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("error", "Edycja nie powiodła się");
			return "failure";	
		}
	}
	
	public String getAllUsers(){
		Map sessionMap= FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
		List users= (List) sessionMap.get("users");
		if(users == null){
			users= UserLogic.getInstance().getUsers();
			sessionMap.put("users", users);
		}
		return "users.jsp?faces-redirect=true";
	}
	
	public String removeUser(){
		boolean result= UserLogic.getInstance().removeUser(selectedUser.id);
		if(result){
			List userList= UserLogic.getInstance().getUsers();
			FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("users", userList);
			return "users.jsp?faces-redirect=true";
			
		}
		else{
			FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("error", "Usuwanie nie powiodło się");
			return "failure";	
		}
	}
	
	public String changeTheme(){
		UserLogic.getInstance().changeTheme(this.loggedUser.id, style);
		return null;
	}
	
	public String getFullName(){
		return name+" "+surname;
	}
	
	public String addUser(){
		if(!password.equals(repeatPass)){
			FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("error", "Hasła nie są takie same");
			return "failure";
		}	
		boolean result= UserLogic.getInstance().addUser(this);
		Map sessionMap= FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
		List users= (List) UserLogic.getInstance().getUsers();
		if(result){
			sessionMap.put("users", users);
			clearData();
			return "success";
		}
		else{
			FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("error", "Dodanie nie powiodło się");
			return "failure";	
		}
	}
	

	private void clearData(){
		this.address= null;
		this.login= null;
		this.password= null;
		this.repeatPass= null;
		this.name= null;
		this.surname= null;
		this.type= null;
	}
	
	
	
}
